var express = require('express');
var pool = require('../mysql');
var router = express.Router();

//搜索
router.get('/', function(req, res, next) {
    let name = req.cookies.user;
    let { search = '' } = req.query;
    if(search == ''){
      pool.query(`select * from component`, function (err, result) {
        if(result){
          pool.query(`select * from label`, function (err, la_result) {
            if(la_result){
              res.render('index', { title: 'React 组件库', data:result, name:name, label: la_result});
            }else{
              res.render('index', { title: 'React 组件库', data:result, name:name, label: [] });
            }
          });
        }else{
          res.render('index', { title: 'React 组件库', name:name, data:[], label: [] });
        }
      });
    }else{
      pool.query(`select component.* from component,label,component_label_relation as re where re.cid = component.id and re.lid = label.id and label.name = '${search}'`, function (err, result) {
        if(result){
          pool.query(`select * from label`, function (err, la_result) {
            if(la_result){
              res.render('index', { title: 'React 组件库', data:result, name:name, label: la_result});
            }else{
              res.render('index', { title: 'React 组件库', data:result, name:name, label: [] });
            }
          });
        }else{
          res.render('index', { title: 'React 组件库', name:name, data:[], label: [] });
        }
      });
    }
});

module.exports = router;